package com.hhu.userservice.controller;

import com.hhu.commonutils.pojo.Result;
import com.hhu.userservice.object.dto.TeacherInfo;
import com.hhu.userservice.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    private TeacherService teacherService;

    @Autowired
    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    /**
     * create by: yaodao
     * description: 获取教师信息
     * create time: 2022/3/9 8:31 下午
     */
    @PostMapping("/queryTeacher")
    @CrossOrigin(origins = "*")
    public Result getTeacherInfo(Object o) {
        try {
            List<TeacherInfo> teacherInfoList = teacherService.queryAllTeachers();
            return Result.ok().Data(teacherInfoList);
        } catch (Exception e) {
            return Result.server_error().Message("查询失败：\n" + e.getMessage());
        }
    }

    @PostMapping("/addTeacher")
    @CrossOrigin(origins = "*")
    public Result getTeacherInfo(@RequestBody List<TeacherInfo> teacherInfos) {
        try {
            teacherService.addTeachers(teacherInfos);
            return Result.ok().Message("新增成功！");
        } catch (Exception e) {
            return Result.server_error().Message("新增失败：\n" + e.getMessage());
        }
    }

    /**
     * 批量删除教师
     * @param teacherInfos
     * @return
     */
    @PostMapping("/deleteTeacher")
    @CrossOrigin(origins = "*")
    public Result deleteTeacher(@RequestBody List<TeacherInfo> teacherInfos) {
        try {
            teacherService.deleteTeachers(teacherInfos);
            return Result.ok().Message("删除成功！");
        } catch (Exception e) {
            return Result.server_error().Message("删除失败：\n" + e.getMessage());
        }
    }

    /**
     * 单笔更新教师信息
     * @param teacherInfo
     * @return
     */
    @PostMapping("/updateTeacher")
    @CrossOrigin(origins = "*")
    public Result updateTeacher(@RequestBody TeacherInfo teacherInfo) {
        try {
            teacherService.updateTeacher(teacherInfo);
            return Result.ok().Message("更新成功！");
        } catch (Exception e) {
            return Result.server_error().Message("更新失败：\n" + e.getMessage());
        }
    }
}
